###################################################################################

library(data.table)
library(tableone) 
library(argparser)

##############################################################################

argp <- arg_parser("Compare Base line")
argp <- add_argument(argp, "--tumor_list", help="")
argp <- add_argument(argp, "--baseline_file", help="")
argp <- add_argument(argp, "--qc_file", help="")
argp <- add_argument(argp, "--purity_file", help="")
argp <- add_argument(argp, "--burden_all_file", help="")
argp <- add_argument(argp, "--burden_cds_file", help="")
argp <- add_argument(argp, "--out_dir", help="Output path")

argv <- parse_args(argp)

tumor_list <- argv$tumor_list
qc_file <- argv$qc_file
baseline_file <- argv$baseline_file
purity_file <- argv$purity_file
burden_all_file <- argv$burden_all_file
burden_cds_file <- argv$burden_cds_file
out_dir <- argv$out_dir

if(1!=1){

    work_dir <- "/public/home/xxf2019/20220915_gastric_multiple/dna_combinePublic"
    tumor_list <- paste0(work_dir , "/config/tumor_normal.class.MSS_MSI.list")
    baseline_file <- paste0( work_dir , "/config/STAD_MutipleReigon_baseline.tsv")
    qc_file <- paste0( work_dir , "/Qc/Summary_Qc.tsv")
    purity_file <- paste0( work_dir , "/titan/Purity_titan.final.tsv")
    burden_all_file <- paste0( work_dir , "/Qc/Burden.coverage10x.Autosomal.txt")
    burden_cds_file <- paste0( work_dir , "/Qc/Burden.coverage10x.Autosomal.cds.txt")
    out_dir <- paste0(work_dir , "/config")

}

###################################################################################

dat_base <- data.frame(fread(baseline_file , header = T))
dat_sample <- data.frame(fread(tumor_list , header = T))
dat_qc <- data.frame(fread(qc_file , header = T))
dat_purity <- data.frame(fread(purity_file , header = T))
dat_burden_all <- data.frame(fread(burden_all_file , header = T))
dat_burden_cds <- data.frame(fread(burden_cds_file , header = T))

###################################################################################

dat <- subset( dat_base , Patient %in% dat_sample$ID )

colnames(dat_burden_all) <- c("Tumor" , "coverage_All")
colnames(dat_burden_cds) <- c("Tumor" , "coverage_CDS")

###################################################################################
## 整理性别和吸烟
dat[which(dat$Tobacco==1),'Tobacco'] <- "Smoke"
dat[which(dat$Tobacco==0),'Tobacco'] <- "No"

dat[which(dat$Alcohol==1),'Alcohol'] <- "Drink"
dat[which(dat$Alcohol==0),'Alcohol'] <- "No"

dat[which(dat$HP==1),'HP'] <- "Positive"
dat[which(dat$HP==0),'HP'] <- "Negative"

dat[which(dat$PickleFood %in% c("A" , "B" , "C" )),'PickleFood'] <- "No"
dat[which(dat$PickleFood %in% c("D" , "E" )),'PickleFood'] <- "Eat"

###################################################################################
## 按照样本合并
dat_qc_out <- merge( dat , dat_qc , by.x = "Patient" , by.y = "ID" )
dat_qc_out <- merge( dat_qc_out , dat_purity , by.x = "Tumor" , by.y = "Sample" )
dat_qc_out <- merge( dat_qc_out , dat_burden_all , by = "Tumor" )
dat_qc_out <- merge( dat_qc_out , dat_burden_cds , by = "Tumor" )
dat_qc_out <- subset(dat_qc_out , Tumor %in% dat_sample$Tumor)

out_file <- paste(out_dir , "/STAD-useCombine.Sample.tsv",sep="")
write.table(dat_qc_out , out_file , sep='\t' , quote = F , row.names=F )

###################################################################################
## 按照病人合并
dat_qc_out <- merge( dat , unique(data.frame(ID = dat_qc$ID , Type = dat_qc$Type) ), by.x = "Patient" , by.y = "ID" )
dat_qc_out[is.na(dat_qc_out)] <- "NA"

out_file <- paste(out_dir , "/STAD-useCombine.Patient.tsv",sep="")
write.table(dat_qc_out , out_file , sep='\t' , quote = F , row.names=F )

###################################################################################
######## 生成基线表
## 三类
dat <- dat_qc_out

myVars <- c("Gender" , "Age" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")
factorVars <- c("Gender" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")

dat$Tobacco <- factor(dat$Tobacco , level = c("Smoke" , "No" , "NA") )
dat$Alcohol <- factor(dat$Alcohol , level = c("Drink" , "No" , "NA") )
dat$HP <- factor(dat$HP , level = c("Positive" , "Negative" , "NA") )
dat$PickleFood <- factor(dat$PickleFood , level = c("Eat" , "No" , "NA") )
dat$Stage <- factor(dat$Stage , level = c("I" , "II" , "III" , "IV" , "NA") )
dat$Type <- factor(dat$Type , level = c("IM + IGC" , "IM + DGC" , "IM + IGC + DGC") )
dat$Age <- as.numeric(dat$Age)

tab <- CreateTableOne(vars = myVars,  
               data = dat , 
               strata = 'Type',
               factorVars = factorVars,
               includeNA = TRUE)
tab <- print(tab, showAllLevels = TRUE, 
    quote = FALSE, # 不显示引号
    noSpaces = TRUE, # 删除用于在R控制台中对齐文本的空格
    catDigits = 2, contDigits = 2, pDigits = 2, # 修改连续变量小数位数为2位,分类变量百分比位数为2位,调整小数位数为2位；
    printToggle = FALSE)

out_file <- paste(out_dir , "/STAD-useCombine.BaseLineCompare.csv",sep="")
write.csv(tab , out_file  )

###################################################################################
######## 生成基线表
## IGC和DGC的比较
dat <- dat_qc_out
dat <- subset(dat , Type !=  "IM + IGC + DGC")

myVars <- c("Gender" , "Age" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")
factorVars <- c("Gender" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")

dat$Tobacco <- factor(dat$Tobacco , level = c("Smoke" , "No" , "NA") )
dat$Alcohol <- factor(dat$Alcohol , level = c("Drink" , "No" , "NA") )
dat$HP <- factor(dat$HP , level = c("Positive" , "Negative" , "NA") )
dat$PickleFood <- factor(dat$PickleFood , level = c("Eat" , "No" , "NA") )
dat$Stage <- factor(dat$Stage , level = c("I" , "II" , "III" , "IV" , "NA") )
dat$Type <- factor(dat$Type , level = c("IM + IGC" , "IM + DGC") )
dat$Age <- as.numeric(dat$Age)

tab <- CreateTableOne(vars = myVars,  
               data = dat , 
               strata = 'Type',
               factorVars = factorVars,
               includeNA = TRUE)
tab <- print(tab, showAllLevels = TRUE, 
    quote = FALSE, # 不显示引号
    noSpaces = TRUE, # 删除用于在R控制台中对齐文本的空格
    catDigits = 2, contDigits = 2, pDigits = 2, # 修改连续变量小数位数为2位,分类变量百分比位数为2位,调整小数位数为2位；
    printToggle = FALSE)

#out_file <- paste(out_dir , "/STAD-useCombine.BaseLineCompare.IGC_DGC.csv",sep="")
#write.csv(tab , out_file  )

###################################################################################
######## 总的基线表
dat <- dat_qc_out

myVars <- c("Gender" , "Age" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")
factorVars <- c("Gender" , "Tobacco" , "Alcohol" , "PickleFood" , "Stage" , "HP")

dat$Tobacco <- factor(dat$Tobacco , level = c("Smoke" , "No" , "NA") )
dat$Alcohol <- factor(dat$Alcohol , level = c("Drink" , "No" , "NA") )
dat$HP <- factor(dat$HP , level = c("Positive" , "Negative" , "NA") )
dat$PickleFood <- factor(dat$PickleFood , level = c("Eat" , "No" , "NA") )
dat$Stage <- factor(dat$Stage , level = c("I" , "II" , "III" , "IV" , "NA") )
dat$Type <- factor(dat$Type , level = c("IM + IGC" , "IM + DGC") )
dat$Age <- as.numeric(dat$Age)

tab <- CreateTableOne(vars = myVars,  
               data = dat , 
               factorVars = factorVars,
               includeNA = TRUE)
tab <- print(tab, showAllLevels = TRUE, 
    quote = FALSE, # 不显示引号
    noSpaces = TRUE, # 删除用于在R控制台中对齐文本的空格
    catDigits = 2, contDigits = 2, pDigits = 2, # 修改连续变量小数位数为2位,分类变量百分比位数为2位,调整小数位数为2位；
    printToggle = FALSE)

#out_file <- paste(out_dir , "/STAD-useCombine.BaseLineCompare.GC.csv",sep="")
#write.csv(tab , out_file  )
